package com.soft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.DigestUtils;

import com.soft.entity.User;
import com.soft.service.MailService;
import com.soft.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@Autowired private MailService mailService;

	@RequestMapping("/queryAllUser")
	public List<User> queryAll(){
		return userService.queryAll();
	}

	// 登录：支持邮箱或手机号
    @RequestMapping("/login")
    public Map<String,Object> login(String account, String userPassword) {
        Map<String,Object> resp = new HashMap<>();
        String encryptedPwd = DigestUtils.md5DigestAsHex(userPassword.getBytes());
        User user = userService.login(account, encryptedPwd); // account可能是邮箱或手机号
        if (user != null) {
            resp.put("state","success");
            resp.put("message", user.getUserEmail()); // 登录成功返回邮箱
        } else {
            resp.put("state","fail");
            resp.put("message","账号或密码错误");
        }
        return resp;
    }

    // 注册：邮箱必填，手机号选填
    @RequestMapping("/registeruser")
    public Map<String,Object> register(String userEmail, String userPhone, String userPassword, String code) {
        Map<String,Object> resp = new HashMap<>();
        boolean verified = mailService.verifyCode(userEmail, code);
        if (!verified) {
            resp.put("state","fail");
            resp.put("message","验证码错误或已过期");
            return resp;
        }
        String encryptedPwd = DigestUtils.md5DigestAsHex(userPassword.getBytes());
        User user = userService.register(userEmail, userPhone, encryptedPwd);
        if (user != null) {
            resp.put("state","success");
            resp.put("message","注册成功");
        } else {
            resp.put("state","fail");
            resp.put("message","注册失败，邮箱已存在");
        }
        return resp;
    }

    // 忘记密码：邮箱验证码验证
    @RequestMapping("/resetPassword")
    public Map<String,Object> resetPassword(String userEmail, String newPassword, String code) {
        Map<String,Object> resp = new HashMap<>();
        boolean verified = mailService.verifyCode(userEmail, code);
        if (!verified) {
            resp.put("state","fail");
            resp.put("message","验证码错误或已过期");
            return resp;
        }
        String encryptedPwd = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        if (userService.resetPassword(userEmail, encryptedPwd)) {
            resp.put("state","success");
            resp.put("message","密码重置成功");
        } else {
            resp.put("state","fail");
            resp.put("message","用户不存在");
        }
        return resp;
    }

    // 发送验证码
    @RequestMapping("/sendCode")
    public Map<String, Object> sendCode(@RequestParam String email) {
        mailService.sendCode(email);
        Map<String, Object> resp = new HashMap<>();
        resp.put("state", "success");
        resp.put("message", "验证码已发送，请查收邮箱");
        return resp;
    }
	 
	// 临时存验证码
	    private Map<String, String> codeCache = new HashMap<>();

	    // 校验验证码
	    @RequestMapping("/verifyCode")
	    public Map<String, Object> verifyCode(@RequestParam String email,
	                                          @RequestParam String code) {
	        Map<String, Object> resp = new HashMap<>();
	        boolean success = mailService.verifyCode(email, code);
	        if (success) {
	            resp.put("state", "success");
	            resp.put("message", "验证码正确");
	        } else {
	            resp.put("state", "fail");
	            resp.put("message", "验证码错误或已过期");
	        }
	        return resp;
	    }
	 
    @RequestMapping("/getUserIdByEmail")
    public Long getUserIdByPhone (String userEmail) {
    	Long userId = userService.getUserIdByEmail(userEmail);
        return userId != null ? userId : 0L; // 返回0或其他表示未找到
    }
    

}